package com.mallcloud.mall.app.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mallcloud.mall.app.service.AppPointsCouponService;
import com.mallcloud.mall.common.core.util.R;
import com.mallcloud.mall.mall.api.entity.MallMyEnableCouponModel;
import com.mallcloud.mall.mall.api.entity.MallMyExpiredCouponModel;
import com.mallcloud.mall.mall.api.entity.MallMyUsedCouponModel;
import com.mallcloud.mall.mall.api.entity.dto.*;
import com.mallcloud.mall.mall.api.entity.vo.MallPointsCouponVO;
import com.mallcloud.mall.mall.service.MallMyEnableCouponService;
import com.mallcloud.mall.mall.service.MallMyExpiredCouponService;
import com.mallcloud.mall.mall.service.MallMyUsedCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "app-积分券接口")
@RestController
@AllArgsConstructor
@RequestMapping("/app/pointsCoupon")
public class AppPointsCouponController {
	private final AppPointsCouponService appPointsCouponService;
	private final MallMyEnableCouponService mallMyEnableCouponService;
	private final MallMyExpiredCouponService mallMyExpiredCouponService;
	private final MallMyUsedCouponService mallMyUsedCouponService;

	//--积分券-------------------------------------------------------------------
	@ApiOperation("券-可买分页")
	@GetMapping("/payPage")
	public R<IPage<MallPointsCouponVO>> payCouponPage(Page page) {
		return R.ok(appPointsCouponService.payCouponPage(page));
	}

	@ApiOperation("券-可领取分页")
	@GetMapping("/freePage")
	public R<IPage<MallPointsCouponVO>> freeCouponPage(Page page) {
		return R.ok(appPointsCouponService.freeCouponPage(page));
	}

	@ApiOperation("券-可兑换分页")
	@GetMapping("/redeemPage")
	public R<IPage<MallPointsCouponVO>> redeemCouponPage(Page page) {
		return R.ok(appPointsCouponService.redeemCouponPage(page));
	}

	@ApiOperation("券详情")
	@GetMapping("/getInfo")
	public R<MallPointsCouponVO> getInfo(MallPointsCouponVO dto) {
		return R.ok(appPointsCouponService.getInfo(dto));
	}

	//--我的券-------------------------------------------------------------------
	@ApiOperation("券-可使用分页")
	@GetMapping("/enablePage")
	public R<IPage<MallMyEnableCouponModel>> enablePage(Page page, MallMyEnableCouponModel dto) {
		return R.ok(mallMyEnableCouponService.page(page, Wrappers.lambdaQuery(dto).orderByDesc(MallMyEnableCouponModel::getGetTime)));
	}

	@ApiOperation("券-可使用列表")
	@GetMapping("/enableList")
	public R<List<MallMyEnableCouponModel>> enableList(MallMyEnableCouponModel dto) {
		return R.ok(mallMyEnableCouponService.list(Wrappers.lambdaQuery(dto).orderByDesc(MallMyEnableCouponModel::getGetTime)));
	}

	@ApiOperation("券-可使用详情")
	@GetMapping("/enableInfo")
	public R<MallMyEnableCouponModel> enableInfo(MallMyEnableCouponModel dto) {
		return R.ok(mallMyEnableCouponService.getOne(Wrappers.lambdaQuery(dto)));
	}

	@ApiOperation("券-已使用分页")
	@GetMapping("/usedPage")
	public R<IPage<MallMyUsedCouponModel>> getUsedPage(Page page, MallMyUsedCouponModel dto) {
		return R.ok(mallMyUsedCouponService.page(page, Wrappers.lambdaQuery(dto).orderByDesc(MallMyUsedCouponModel::getGetTime)));
	}

	@ApiOperation("券-过期分页")
	@GetMapping("/expiredPage")
	public R<IPage<MallMyExpiredCouponModel>> getExpiredPage(Page page, MallMyExpiredCouponModel dto) {
		return R.ok(mallMyExpiredCouponService.page(page, Wrappers.lambdaQuery(dto).orderByDesc(MallMyExpiredCouponModel::getGetTime)));
	}


	//--我的券操作-------------------------------------------------------------------
	@ApiOperation("券-可使用券-批量核销")
	@PostMapping("/enable/batchWriteOff")
	public R<String> batchWriteOff(@RequestBody ProCouponBatchWriteOff dto) {
		return R.ok(mallMyEnableCouponService.proCouponBatchWriteOff(dto));
	}

	@ApiOperation("券-兑换")
	@PostMapping("/redeem")
	public R redeem(@RequestBody ProRedeemCoupon dto) {
		appPointsCouponService.proRedeemCoupon(dto);
		return R.ok();
	}

	@ApiOperation("券-领取")
	@PostMapping("/get")
	public R get(@RequestBody ProGetCoupon dto) {
		appPointsCouponService.proGetCoupon(dto);
		return R.ok();
	}

	@ApiOperation("券-可买取检查")
	@PostMapping("/checkPay")
	public R<String> checkPay(@RequestBody ProPayCoupon dto) {
		return R.ok(appPointsCouponService.proCheckPayCoupon(dto));
	}

	@ApiOperation("券-转赠")
	@PostMapping("/given")
	public R given(@RequestBody ProGivenCoupon dto) {
		appPointsCouponService.proGivenCoupon(dto);
		return R.ok();
	}
}